package com.comunity_cloud.client.view2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import com.comunity_cloud.client.controller.MainController2;

public class LoginFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4528312618850203454L;
	private JPasswordField passwordTxt;
	private JTextField userNameTxt;
	private JButton okBtn;
	private JButton cancelBtn;
	private JLabel errorLbl;
	
	private MainController2 controller;
	private JProgressBar progress;
	
	public LoginFrame() {
		
		Container contentPane = getContentPane();
		
		contentPane.setLayout(new BorderLayout());
		
		errorLbl = new JLabel();
		
		contentPane.add(errorLbl,BorderLayout.NORTH);
		errorLbl.setForeground(Color.red);
//		errorLbl.setVisible(false);
		
		progress = new JProgressBar();
		
		progress.setIndeterminate(true);
		contentPane.add(progress,BorderLayout.NORTH);
		
		progress.setVisible(false);
		
		JPanel form = new JPanel();
		contentPane.add(form,BorderLayout.CENTER);
		
        SpringLayout layout = new SpringLayout();
        form.setLayout(layout);
        JLabel userNameLbl = new JLabel("User name: ", JLabel.TRAILING);
		form.add(userNameLbl);
        
        userNameTxt = new JTextField("", 15);
        userNameLbl.setLabelFor(userNameTxt);
        form.add(userNameTxt);
        JLabel passwordLbl = new JLabel("Password: ",JLabel.TRAILING);
		form.add(passwordLbl);
		
        passwordTxt = new JPasswordField("", 15);
        passwordLbl.setLabelFor(passwordTxt);
        form.add(passwordTxt);
        
        SpringUtilities.makeCompactGrid(form,
                2, 2, //rows, cols
                6, 6,        //initX, initY
                6, 6);       //xPad, yPad
        
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        
        okBtn = new JButton("OK");
        
        okBtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				okBtnClicked();
			}
		});
        
        cancelBtn = new JButton("Cancel");
        
        buttonsPanel.add(okBtn);
        buttonsPanel.add(cancelBtn);
        
        JLinkButton button = new JLinkButton();
        button.setText("Register");
        
        buttonsPanel.add(button);
        contentPane.add(buttonsPanel,BorderLayout.SOUTH);
        pack();
        
        passwordTxt.addKeyListener(new KeyAdapter() {
        	@Override
        	public void keyPressed(KeyEvent e) {
        		passwordTxtKeyPressed(e);
        	}
		});
	}
	
	protected void passwordTxtKeyPressed(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_ENTER){
			okBtnClicked();
		}
	}

	@SuppressWarnings("deprecation")
	protected void okBtnClicked() {
		controller.login(userNameTxt.getText(),passwordTxt.getText());
	}

	public void showError(final String string) {

		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				errorLbl.setText(string);
				errorLbl.setVisible(true);
				progress.setVisible(false);
				repaint();
			}
		});				
		
	}

	protected void openRegisterForm() {
		
	}

	public String getUserName(){
		return userNameTxt.getText(); 
	}
	
	@SuppressWarnings("deprecation")
	public String getPassword(){
		return passwordTxt.getText();
	}

	public void setController(MainController2 mainController2) {
		this.controller = mainController2;
	}	
	
	public void enableForm(final boolean enable){
		System.out.println(enable);
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				userNameTxt.setEnabled(enable);
				passwordTxt.setEnabled(enable);
				okBtn.setEnabled(enable);
				cancelBtn.setEnabled(enable);
				progress.setVisible(!enable);
				if (!enable) {
					errorLbl.setVisible(false);
				}
			}
		});
	}
}
